package facetest.az26;

import org.junit.Test;

import java.util.concurrent.LinkedTransferQueue;
import java.util.concurrent.TransferQueue;

/**
 * All rights Reserved, Designed By www.rongdasoft.com
 *
 * @version V1.0
 * @Title: TransferQueueTest
 * @Description:
 * @author:wsp
 * @date: 2019/10/12
 * @Copyright: 2019/10/12 www.rongdasoft.com
 * Inc. All rights reserved.
 */
public class TransferQueueTest {
    static Thread t1, t2;
    String[] zimuArr = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    String[] numArr = new String[zimuArr.length];
    TransferQueue<String> transferQueue = new LinkedTransferQueue<>();


    {
        for (int i = 0; i < zimuArr.length; i++) {
            numArr[i] = (i + 1) + "";
        }

    }

    @Test
    public void test() {

        t1 = new Thread(() -> {
            for (String s : zimuArr) {
                try {
                    transferQueue.take();
                    System.out.println(s);
                    transferQueue.transfer(s);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }


        });
        t2 = new Thread(() -> {
            for (String s : numArr) {
                try {
                    transferQueue.transfer(s);
                    transferQueue.take();
                    System.out.println(s);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }


        });
        t2.start();
        t1.start();

        while (true) ;
    }
}